package Panel;

import Bean.OrderGoods;
import Dao.orderGoodsDao;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class OrderGoodsPanel extends JPanel {
    //添加采购窗体测试方法
    private JFrame j;
    private JTextField oidInp;
    private JLabel oid;
    private JTextField custInp;
    private JTextField deDateInp;
    private JTextField numInp;
    private JTextField priceInp;
    private JTextField goodsNameInp;


//    public static void main(String[] args) {
//        OrderGoodsPanel OrderGoodsPanel = new OrderGoodsPanel();
//        OrderGoodsPanel.showAddOrderModel(2);;
//    }
    public OrderGoodsPanel() {
        this.setLayout(null);
        this.setBackground(new Color(66,203,222));
        this.setBounds(0, 100, 680, 365);
        table(orderGoodsDao.getOrderGoods());
        this.setVisible(true);
    }

    public void table(List<OrderGoods> l) {
        JLabel qTil = new JLabel("查询条件:");
        qTil.setBounds(90, 0, 100, 30);
        String[] files = new String[]{"客户", "订单号", "交货日期"};
        JComboBox cb = new JComboBox(files);
        cb.setSelectedIndex(0);
        cb.setBounds(150, 5, 100, 20);
        JTextField query = new JTextField();
        query.setBounds(280, 5, 200, 25);
        JButton qBtn = new JButton("搜索");
        qBtn.setBounds(500, 5, 80, 20);
        String[] title = {"是否入库", "编号", "客户", "订单号", "交货日期", "商品名", "金额", "数量"};
        //获取数据
        List<OrderGoods> list = l;
        String[][] con = new String[list.size()][8];
        for (int i = 0; i <list.size() ; i++) {
            con[i][0] = "已经入库";
            con[i][1] = String.valueOf(list.get(i).getId());
            con[i][2] = String.valueOf(list.get(i).getCust());
            con[i][3] = String.valueOf(list.get(i).getOid());
            con[i][4] = String.valueOf(list.get(i).getDeDate());
            con[i][5] = String.valueOf(list.get(i).getGoodsName());
            con[i][6] = String.valueOf(list.get(i).getPrice());
            con[i][7] = String.valueOf(list.get(i).getNum());
        }

        JTable table = new JTable(con, title);
        table.setBounds(0, 0, 600, 200);
        JScrollPane jsp = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        jsp.setBounds(40, 80, 600, 200);

        //操作数据 影响数据库
        qBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println(cb.getSelectedItem());

                List<OrderGoods> query1 = orderGoodsDao.query( cb.getSelectedItem().toString(), query.getText());
                table(query1);
            }
        });
        JButton add = new JButton("添加");
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //弹出增加采购订购窗体
                showAddOrderModel(1);
            }
        });
        JButton modify = new JButton("修改");
        modify.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //弹出修改购订购窗体
                showAddOrderModel(2);
            }
        });
        JButton del = new JButton("删除");
        del.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //弹出删除购订购窗体
                showAddOrderModel(3);
            }
        });
        add.setBounds(150,300,80,20);
        modify.setBounds(260,300,80,20);
        del.setBounds(370,300,80,20);
        this.add(del);
        this.add(modify);
        this.add(add);
        this.add(jsp);
        this.add(qBtn);
        this.add(query);
        this.add(cb);
        this.add(qTil);
    }
    public void showAddOrderModel(int where){
        j = new JFrame();
        j.setLayout(null);
        j.setBounds(450,300,600,300);
        oid =  new JLabel("订单号:");
        JLabel deDate = new JLabel("交货日期:");
        JLabel num = new JLabel("数  量:");
        JLabel cust = new JLabel("客  户:");
        JLabel goodsName = new JLabel("货物名称:");
        JLabel price = new JLabel("金额:");
        oidInp = new JTextField();
         deDateInp = new JTextField();
         numInp = new JTextField();
         custInp = new JTextField();
         goodsNameInp = new JTextField();
         priceInp = new JTextField();
        oid.setBounds(30,40,80,20);
        oidInp.setBounds(80,40,150,20);
        deDate.setBounds(20,80,80,20);
        deDateInp.setBounds(80,80,150,20);
        num.setBounds(40,120,80,20);
        numInp.setBounds(80,120,150,20);
        cust.setBounds(300,40,80,20);
        custInp.setBounds(350,40,150,20);
        goodsName.setBounds(280,80,80,20);
        goodsNameInp.setBounds(350,80,150,20);
        price.setBounds(300,120,80,20);
        priceInp.setBounds(350,120,150,20);
        JButton confirm = new JButton();
        JButton exit = new JButton("退出");
        confirm.setBounds(120,200,80,20);
        exit.setBounds(300,200,80,20);
        //添加
        if(where==1){
            j.setTitle("添加采购订货窗体");
            confirm.setText("添加");
            //添加操作
            confirm.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    confirmOp("添加");
                }
            });
            //修改
        }else if(where==2){
            j.setTitle("修改采购订货窗体");
            confirm.setText("修改");
            confirm.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    confirmOp("修改");
                }
            });
            //删除
        }else if(where ==3){
            j.setTitle("删除采购订货窗体");
            confirm.setText("删除");
            confirm.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    del();
                }
            });
        }


        //退出操作
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                j.dispose();
            }
        });
        //删除窗体只有订单输入框
        if(where==3){
            j.add(confirm);
            j.add(exit);
            j.add(oidInp);
            j.add(oid);
    }else{
            j.add(confirm);
            j.add(exit);
            j.add(oidInp);
            j.add(oid);
            j.add(price);
            j.add(priceInp);
            j.add(goodsName);
            j.add(goodsNameInp);
            j.add(cust);
            j.add(custInp);
            j.add(num);
            j.add(numInp);
            j.add(deDate);
            j.add(deDateInp);j.add(price);
            j.add(priceInp);
            j.add(goodsName);
            j.add(goodsNameInp);
            j.add(cust);
            j.add(custInp);
            j.add(num);
            j.add(numInp);
            j.add(deDate);
            j.add(deDateInp);j.add(price);
            j.add(priceInp);
            j.add(goodsName);
            j.add(goodsNameInp);
            j.add(cust);
            j.add(custInp);
            j.add(num);
            j.add(numInp);
            j.add(deDate);
            j.add(deDateInp);j.add(confirm);
            j.add(exit);
            j.add(oidInp);
            j.add(oid);
            j.add(price);
            j.add(priceInp);
            j.add(goodsName);
            j.add(goodsNameInp);
            j.add(cust);
            j.add(custInp);
            j.add(num);
            j.add(numInp);
            j.add(deDate);
            j.add(deDateInp);j.add(price);
            j.add(priceInp);
            j.add(goodsName);
            j.add(goodsNameInp);
            j.add(cust);
            j.add(custInp);
            j.add(num);
            j.add(numInp);
            j.add(deDate);
            j.add(deDateInp);j.add(price);
            j.add(priceInp);
            j.add(goodsName);
            j.add(goodsNameInp);
            j.add(cust);
            j.add(custInp);
            j.add(num);
            j.add(numInp);
            j.add(deDate);
            j.add(deDateInp);
        }
        j.setVisible(true);
    }
    //确认操作
    public void confirmOp(String op){

           Boolean isSuccess = null;
           try {
                   int o = Integer.parseInt(oidInp.getText());
                   float p = Float.parseFloat(priceInp.getText());
                   int n = Integer.parseInt(numInp.getText());
               if("添加".equals(op)) {
                   isSuccess = orderGoodsDao.addOrderGoods(new OrderGoods(0, custInp.getText(), o, deDateInp.getText(), goodsNameInp.getText(), p, n));
               }else if("修改".equals(op)){
                   //根据订单号修改数据
                   isSuccess = orderGoodsDao.modify(new OrderGoods(0, custInp.getText(), o, deDateInp.getText(), goodsNameInp.getText(), p, n));
               }
           }
           catch (NumberFormatException e){
               JOptionPane.showMessageDialog(j,"请输入正确类型的数据");
               return;
           }
           if(isSuccess){
               JOptionPane.showMessageDialog(j,op+"成功");
               table(orderGoodsDao.getOrderGoods());
               j.dispose();
           }else{
               JOptionPane.showMessageDialog(j,op+"失败 请重试");
           }
       }

       //另外的单独方法 实现删除数据
    public void del(){
        //根据订单号删除数据
        Boolean isSuccess = null;
        try {
            int o = Integer.parseInt(oidInp.getText());
            isSuccess = orderGoodsDao.delete(o);
        }catch (NumberFormatException e){
            JOptionPane.showMessageDialog(j,"请输入正确类型的数据");
            return;
        }
        if(isSuccess){
            JOptionPane.showMessageDialog(j,"删除成功");
            table(orderGoodsDao.getOrderGoods());
            j.dispose();
        }else{
            JOptionPane.showMessageDialog(j,"删除失败 请重试");
        }

    }

}
